﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
using System.Collections.Generic;

public partial class AdminX_Video_VideoMain : System.Web.UI.UserControl
{
    private int nDefaulanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        //Alert1.HideMessage();

        if (!IsPostBack)
        {
            this.ShowMess();
            this.SetMullanguage();
            this.BindDataToLanguageDropDown();
            this.BindDataToGrid(1);
        }
    }

    
    private void SetMullanguage()
    {
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaulanguage - 1];
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaulanguage - 1];
    }

    #region  Message
 

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

    #region BindDataToDropDown

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #endregion

    #region Grid processing


    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {  
        BindDataToGrid(1);
    }

    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nItemView = 0;
        int nItemID = 0;
        string sSearch ="";

        int nStatus = -1;
        int nLanguageID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;
        int nDefaultLanguage = Support.GetDefaultLanguage();

        DataTable dtbVideo = new DataTable();
        Video objVideo = new Video();
        dtbVideo = objVideo.Search(nItemView, nItemID, sSearch, nDefaultLanguage, nLanguageID, nStatus, CurrentPageIndex, nPageSize, ref nPageCount);
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];

        RpVideo.DataSource = dtbVideo;
        RpVideo.DataBind();        
    }

    protected void RpBanner_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Video objVideo = new Video();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objVideo.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[1]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[1]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_VIDEO_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objVideo.UpdateStatus(nID, EnumeType.INACTIVE);
                //if (nActive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[1]);

                //else
                //    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[1]);
                BindDataToGrid(1);
                break;

            case "Inactive":
                int nInactive = objVideo.UpdateStatus(nID, EnumeType.ACTIVE);
                //if (nInactive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[1]);
                //else
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[1]);
                BindDataToGrid(1);
                break;
        }
    }
    protected void RpBanner_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nCurrentLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");

            Label lblDateModified = (Label)e.Item.FindControl("lblDateModified");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            Label lblID = (Label)e.Item.FindControl("lblID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "VideoID"));

            //lblID.Text = ConvertData.ConvertToString(nID);
            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkDelete.Enabled = true;
            lnkActive.Enabled = true;



            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));

            string sItemTitle = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoTitle"));
            if (sItemTitle == "")
                sItemTitle = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoTitle_default"));
            if (sItemTitle.Length > 80)
                sItemTitle = ConvertData.TruncateString(sItemTitle, 80) + Config.DOT;
            lblTitle.Text = sItemTitle;

            lblDateModified.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }
            string nPriority = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));

            txtPriority.Text = nPriority;

            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nCurrentLangID]);

            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nCurrentLangID]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoID"));

        }
    }
    #endregion

    #region Click Events

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_VIDEO_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    #endregion

    //protected void btnUpdate_Click(object sender, EventArgs e)
    //{


    //    // Update  data
    //    int nCount = dtgVideo.Items.Count;

    //    for (int i = 0; i < nCount; i++)
    //    {
    //        TextBox txtPriority = (TextBox)dtgVideo.Items[i].FindControl("txtPriority");
    //        Label lblID = (Label)dtgVideo.Items[i].FindControl("lblID");

    //        int nVideoID = ConvertData.ConvertToInt(lblID.Text);
    //        int nPriority = ConvertData.ConvertToInt(txtPriority.Text);

    //        Video objVideo = new Video();
    //        int nResult = objVideo.UpdatePriority(nVideoID, nPriority);

    //        if (nResult > 0) { Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[1]); }
    //        else { Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[1]); }
    //    }

    //}
  
}